package com.example.springbatchdemo.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Classname JobDemo
 * @Description TODO
 * @Date 2020/9/19 12:16
 * @Created by kevin
 */
@Configuration
@EnableBatchProcessing
public class JobDemo {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job jobDemoJob() {
        return jobBuilderFactory.get("helloWorldJob")
                .start(step1())
                .next(step2())
                .next(step3())
                .build();
    }

    @Bean
    public Step step3() {
        return stepBuilderFactory.get("step3").tasklet(( StepContribution contribution, ChunkContext chunkContext ) -> {
            System.out.println("执行了step3......");
            return RepeatStatus.FINISHED;
        }).build();
    }

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2").tasklet(( StepContribution contribution, ChunkContext chunkContext ) -> {
            System.out.println("执行了step2......");
            return RepeatStatus.FINISHED;
        }).build();
    }

    public Step step1() {

        return stepBuilderFactory.get("step1").tasklet(( StepContribution contribution, ChunkContext chunkContext ) -> {
            System.out.println("执行了step1......");
            return RepeatStatus.FINISHED;
        }).build();
    }
}
